package com.luban;

import com.luban.entity.User;
import com.luban.service.*;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.ArrayList;
import java.util.List;

public class Test {

	public static void main(String[] args) {

		//1.
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
//		UserService userService = applicationContext.getBean("userService", UserService.class);
//		userService.test();
//		System.out.println("匡振曦");

		//2.
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//		XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);
//		System.out.println(xmlBeanDefinitionReader.loadBeanDefinitions("spring.xml"));

		//3.
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//		AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(applicationContext);
//		reader.register(User.class);  //User上没有@Component啥的修饰，用来注册一个对象的beandefinition
//		applicationContext.refresh();  //这里才会bd变成bean
//		System.out.println(applicationContext.getBean("user"));

		//4. 扫描
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//		ClassPathBeanDefinitionScanner beanDefinitionScanner = new ClassPathBeanDefinitionScanner(applicationContext);
//		beanDefinitionScanner.scan("com.luban.entity");  //扫描得到beanDefinition
//		applicationContext.refresh(); //根据bd生成bean
//		System.out.println(applicationContext.getBean("person"));

		//5. 环境变量
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//		ConfigurableEnvironment environment = applicationContext.getEnvironment();
//		System.out.println(environment.getSystemEnvironment());

		//spring容器（bean对象）（容器最小的就是单例池--》然后是BeanFactory--》最大是ApplicationContext）


		//6. 类型转换  string转成User对象
//		StringToUserPropertyEditor propertyEditor = new StringToUserPropertyEditor();
//		propertyEditor.setAsText("kzx");
//		User value = (User)propertyEditor.getValue();
//		System.out.println(value);
//		System.out.println(value.getName());
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
////		applicationContext.register(UserService.class);
//		UserService userService = applicationContext.getBean("userService", UserService.class);
//		System.out.println(userService.userTest.getName());

		//7. 测试FactoryBean
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
//		System.out.println(applicationContext.getBean("testFactoryBean"));
//		System.out.println(applicationContext.getBean("testFactoryBean"));
//		System.out.println(applicationContext.getBean("testFactoryBean"));
//		System.out.println(applicationContext.getBean("&testFactoryBean")); //就能取到TestFactoryBean对象


		//8. 测试抽象bd
//		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//
//		System.out.println(applicationContext.getBean("userService"));


		//9. 从getBean去看bean是怎么拿到的
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
//		System.out.println(applicationContext.getBean("userService"));

		//10. 依赖注入
//		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//		UserService userService = (UserService)applicationContext.getBean("userService4");
//		userService.test2();

		//11. spring启动流程
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
//		UserService2 userService2 = (UserService2)applicationContext.getBean("userService2");
//		System.out.println(userService2);

		// 12. 测试顺序 @Order
//		List<BaseInterface> baseInterfaces = new ArrayList<BaseInterface>();
//		baseInterfaces.add(new A());
//		baseInterfaces.add(new B());
//		baseInterfaces.sort(new AnnotationAwareOrderComparator()); //这里才会排序
//		for(BaseInterface baseInterface:baseInterfaces) {
//			System.out.println(baseInterface.getClass().getName());
//		}


		//13. 测试循环依赖
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
		AService aService = applicationContext.getBean(AService.class);
		System.out.println(aService);

	}
}


//abstract class A {
//
//}
//abstract class B extends A {
//
//}
